module ProxyPump
  class UserAgents

    include Enumerable

    def initialize(yml)
      load yml
    end

    %w(chrome safari firefox ie).each do |browser|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{browser}
          all[:#{browser}] ||= []
          all[:#{browser}]
        end
      RUBY
    end

    def random
      (chrome | safari | firefox | ie).sample
    end

    def [](key)
      all[:"#{key}"]
    end

    def all
      agents.fetch(:user_agents, {})
    end

    def browsers
      all.keys
    end

    def agents
      @agents ||= {}
    end

    def each(&block)
      all.each(&block)
    end

    def load(yml, &block)
      self.tap { @agents = YAML.load(File.read(yml)).deep_symbolize_keys! }
    end

  end # UserAgents
end # ProxyPump
